package sort2;

import java.util.Stack;

public class Sort {

    public static void insertSort(int[] arr){
        for (int i = 1; i < arr.length; i++) {
            int tmp=arr[i];
            int j=0;
            for(j=i-1;j>=0;j--){
                if (arr[j]>tmp){
                    arr[j+1]=arr[j];
                }else {
                    arr[j+1]=tmp;
                    break;
                }
            }
            if(j<0){
                arr[j+1]=tmp;
            }
        }
    }

    public static void shellSort(int[] arr){
        int gap=arr.length;
        while (gap>1){
            gap/=2;
            shell(arr,gap);
        }
    }

    private static void shell(int[] arr, int gap) {
        for (int i = 1; i < arr.length; i++) {
            int tmp=arr[i];
            int j=i-gap;
            for(;j>=0;j-=gap){
                if (arr[j]>tmp){
                    arr[j+gap]=arr[j];
                }else{
                    arr[j+gap]=tmp;
                    break;
                }
            }
            if (j<0){
                arr[j+gap]=tmp;
            }
        }
    }

    public static void quickSort(int[] arr){
        quickNor(arr,0,arr.length-1);
    }

    public static void mergeSort(int[] arr){
        mergeTmp(arr,0,arr.length-1);
    }

    public static void mergeNor(int[] arr){
        int gap=1;
        while (gap<arr.length){
            for (int i = 0; i < arr.length; i++) {
                int left=i;
                int mid=left+gap-1;
                //处理mid越界的情况
                if (mid>=arr.length){
                    mid=arr.length-1;
                }
                int right=mid+gap;
                //除了right越界的情况
                if (right>=arr.length){
                    right=arr.length-1;
                }
                merge(arr,left,mid,right);
            }
            gap*=2;
        }
    }

    private static void mergeTmp(int[] arr, int left, int right) {
        if (left==right){
            return;
        }
        //分解
        int mid=(left+right)/2;
        mergeTmp(arr,left,mid);
        mergeTmp(arr,mid+1,right);
        //合并
        merge(arr,left,mid,right);
    }

    private static void merge(int[] arr, int left, int mid, int right) {
        int s1=left;
        int e1=mid;
        int s2=mid+1;
        int e2=right;
        int k=0;
        int[] tmp=new int[right-left+1];
        while (s1<=e1 && s2<=e2){
            if (arr[s1]<=arr[s2]){
                tmp[k++]=arr[s1++];
            }else{
                tmp[k++]=arr[s2++];
            }
        }
        while (s1<=e1){
            tmp[k++]=arr[s1++];
        }
        while (s2<=e2){
            tmp[k++]=arr[s2++];
        }
        for (int i = 0; i < k; i++) {
            arr[i+left]=tmp[i];
        }
    }

    private static void quickNor(int[] arr, int start, int end){
        int pivot=partition(arr,start,end);
        Stack<Integer> stack=new Stack<>();
        if (pivot>start+1){
            stack.push(start);
            stack.push(pivot-1);
        }
        if (pivot<end-1){
            stack.push(pivot+1);
            stack.push(end);
        }
        while (!stack.isEmpty()){
            end=stack.pop();
            start=stack.pop();
            pivot=partition(arr,start,end);
            if (pivot>start+1){
                stack.push(start);
                stack.push(pivot-1);
            }
            if (pivot<end-1){
                stack.push(pivot+1);
                stack.push(end);
            }
        }
    }

    private static void quick(int[] arr, int start, int end) {
        if (start>=end){
            return;
        }
        if (start-end+1<3){
            insertSortRange(arr,start,end);
        }
        int mid=findMid(arr,start,end);
        swap(arr,start,mid);
//        System.out.println("start: "+start+" end: "+end);
        int pivot = partition(arr,start,end);
        quick(arr,start,pivot-1);
        quick(arr,pivot+1,end);
    }

    private static void insertSortRange(int[] arr, int start, int end) {
        for (int i = start; i <= end; i++) {
            int tmp=arr[i];
            int j=i-1;
            for (;j>=start;j--){
                if (arr[j]>tmp){
                    arr[j+1]=arr[j];
                }else {
                    arr[j+1]=tmp;
                    break;
                }
            }
            arr[j+1]=tmp;
        }
    }

    /**
     * 三数取中
     * @param arr
     * @param left
     * @param right
     * @return
     */
    private static int findMid(int[] arr, int left, int right) {
        int mid=(left+right)/2;
        if (arr[left]<arr[right]){
            if (arr[mid]>arr[right]){
                return right;
            }else if(arr[mid]<arr[left]){
                return left;
            }else {
                return mid;
            }
        }else {
            if (arr[mid]>arr[left]){
                return left;
            }else if (arr[mid]<arr[right]){
                return right;
            }else {
                return mid;
            }
        }
    }

    public static int partition(int[] arr,int left,int right){
        int tmp=arr[left];
        while (left<right){
            while (left<right&&arr[right]>=tmp){
                right--;
            }
            arr[left]=arr[right];
            while (left<right&&arr[left]<=tmp){
                left++;
            }
            arr[right]=arr[left];
        }
        arr[left]=tmp;
        return left;
    }

    private static int partitionHoare(int[] arr, int left, int right) {
        int tmp=arr[left];
        int tmpLeft=left;
        while (left<right){
            while (left<right&&arr[right]>=tmp){
                right--;
            }
            while (left<right&&arr[left]<=tmp){
                left++;
            }
            swap(arr,left,right);
        }
        swap(arr,left,tmpLeft);
        return left;
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp=arr[i];
        arr[i]=arr[j];
        arr[j]=tmp;
    }

}
